import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import java.net.*;
import java.rmi.*;

public class Client extends JFrame implements ClientInterface, InternalFrameListener,passwordInterface
{
	    	

    public Client() 
    {
    	 	super(" RMI- Examination & Voting System"); 
    	 	
        	      
         	
        	addWindowListener(new WindowAdapter() 
      		  {
               	 public void windowClosing(WindowEvent e) 
              	  {
                	System.exit(0);
               	 }
       		 }
       		 );
       		 
        setContentPane(desktop);
        
        //.setEnabled(false);
        try {
	FileReader fr = new FileReader("bin/server.conf");	        
	BufferedReader br = new BufferedReader(fr);
	String s;
	while((s = br.readLine()) != null ) {
		clientconf.setText(s);
	}
	fr.close(); 
}
catch(IOException ioexceptio)
{
	JOptionPane.showMessageDialog(this,"Can not read the Client Configuration File","Error",JOptionPane.ERROR_MESSAGE);
}

try {
		InetAddress thispc = InetAddress.getLocalHost();
		byte[] add = thispc.getAddress();
		//System.out.println("Address :");
		String s = "";
		for(int i = 0; i < add.length; i++) {
			int unsignbyte = add[i] < 0 ? add[i] + 256 : add[i];
			s += Integer.toString(unsignbyte)+ ".";
		}
		clientip.setText(s);
	}
	catch(Exception ex) {
		JOptionPane.showMessageDialog(this,"Cannot calculate the IP address..","Error",JOptionPane.ERROR_MESSAGE);
		
	}
	
	
	
	
        setJMenuBar(createMenuBar());
	  try
		{
			UIManager.setLookAndFeel(looks[1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
        
    	
    	setBounds(0,0,800,575);
        }   
	
	 
	
	protected JMenuBar createMenuBar()
{
	 menuItemhelp1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"Sorry Help will supply Later\n It's a Demo Client \n For Help Contact to the Administrator.");
            
            }
        });
        
         menuItemhelp3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
         	aboutUs aboutus = new aboutUs();
         	aboutus.setVisible(true);
        	desktop.add(aboutus);
        	try {
            	aboutus.setSelected(true);
        	} catch (java.beans.PropertyVetoException ex) {}
            
            }
        });
         menuItem3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              password app = new password();
              app.setVisible(true);
              desktop.add(app);
              try {
            	app.setSelected(true);
        	} catch (java.beans.PropertyVetoException ex) {}
            
            }
        });
          menuItem3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              }
        });
         
         menuItem6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int confirm = JOptionPane.showConfirmDialog(null, 
			"Are you sure want to Log Out?", 
			"Log Out", 
			JOptionPane.YES_NO_OPTION);
 				
 			if( confirm == JOptionPane.YES_OPTION )
 			{
 				try
			{
			String addServerUrl = "rmi://"+clientconf.getText()+"/MainServer" ;
			MainServerInterface se = (MainServerInterface)Naming.lookup(addServerUrl);
			
			String ra5 = se.ip_address(clientip.getText(),usersName.getText(),"Logged Out");
			
							
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(null,"Cannot contact to the server","Information",JOptionPane.ERROR_MESSAGE);
		}
 				menuItem6.setEnabled(false);
 				menuItem3.setEnabled(true);
 				exammenu.setEnabled(false); 				
 			}
                        if(confirm == JOptionPane.NO_OPTION)
                        return;
            }
        });
         menuItem5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	int confirm = JOptionPane.showConfirmDialog(null, 
			"Are you sure want to exit?", 
			"Exit Application", 
			JOptionPane.YES_NO_OPTION);
 				
 			if( confirm == JOptionPane.YES_OPTION )
 			{
 				JOptionPane.showMessageDialog( null, 
 				"Thanks for using My System");
 				dispose();
 				System.exit(0);
 				
 			}
            }
        });  
        ExamItem1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		LabExam();
        		exammenu.setEnabled(false);
        		votemenu.setEnabled(false);
        	}
        }
        );
        ExamItem2.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		MCQTest();
        		exammenu.setEnabled(false);
        		votemenu.setEnabled(false);
        }
        }
        );
       
        VoteItem1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e)      {
        		
        		Vote();
        		exammenu.setEnabled(false);
        		votemenu.setEnabled(false);
        	}
        }
        );
       VoteItem2.addActionListener(new ActionListener() {
       	public void actionPerformed(ActionEvent e) {
       		Vote2();
       		exammenu.setEnabled(false);
       		votemenu.setEnabled(false);
       	}
	}
	);
        
        		
                   
        
       filemenu.add(menuItem3);
       filemenu.add(menuItem6);
       menuItem6.setEnabled(false);
       filemenu.addSeparator();
       filemenu.add(menuItem5);
       
       exammenu.add(ExamItem1);
       exammenu.addSeparator();
       exammenu.add(ExamItem2);
      
       
       votemenu.add(VoteItem1);
       votemenu.addSeparator();
       votemenu.add(VoteItem2);
      
       
       helpmenu.add(menuItemhelp1);
       helpmenu.addSeparator(); 
       helpmenu.add(menuItemhelp3);
        
        menuBar();   
        return menuBar1;
    }
    public void menuBar() 
    {
	menuBar1.add(filemenu);
        menuBar1.add(exammenu);
        exammenu.setEnabled(false);
        menuBar1.add(votemenu);
        menuBar1.add(helpmenu);
	
         
    }
    
    protected void LabExam() {
    	LabExam te = new LabExam();
				
				desktop.add(te);
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
    	
	}
    
    protected void MCQTest() {
    	MCQTest te = new MCQTest();
				
				desktop.add(te);
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
        }
protected void Vote(){
	Vote te = new Vote();
				
				desktop.add(te);
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
}
protected void Vote2(){
	Vote2 te = new Vote2();
				
				desktop.add(te);
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
}
	
	
    public void internalFrameClosing(InternalFrameEvent e) {
    	exammenu.setEnabled(true);
        votemenu.setEnabled(true);
        
    }

    public void internalFrameClosed(InternalFrameEvent e) {
    	exammenu.setEnabled(true);
        votemenu.setEnabled(true);
    	
    }

    public void internalFrameOpened(InternalFrameEvent e) {
    	
    }

    public void internalFrameIconified(InternalFrameEvent e) {
    	
        
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
    	
        
    }

    public void internalFrameActivated(InternalFrameEvent e) {
    	
    	
        
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
    	
        
    }	
		
	
	
       public static void main(String[] args) 
	   {
        Client frame = new Client();
        frame.setVisible(true);
      
}
}	